<?php

class Controllers_dsgame extends RestController {

  var $sign = ''; // sign is string
  var $data = ''; // data is string

  public function get() {
    try {
      $this->processRest (); 
      // throw new DSException ( - 999, 'ERROR', 'GET request is not support' );
    } catch ( DSException $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->__toArray ();
    }
  }

  public function post() {
    try {
      $this->processRest (); 
      // throw new DSException ( - 999, 'ERROR', 'POST request is not support' );
    } catch ( DSException $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->__toArray ();
    }
  }

  public function put() {
    try {
      // $this->processRest (); 
      // throw new DSException ( - 999, 'ERROR', 'PUT request is not support' );
    } catch ( DSException $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->__toArray ();
    }
  }

  public function delete() {
    try {
      // $this->processRest (); 
      // throw new DSException ( - 999, 'ERROR', 'DELETE request is not support' );
    } catch ( DSException $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->__toArray ();
    }
  }

  public function checkAuth() {

    // get partner config
    global $partner_array;

    // get request data
    $this->data = $this->request ['params'];

    // validate data and session
    if (is_array ( $this->data ) && !empty($_SESSION['user']['fid']) ) { 
      // if data is not invalid
      if (isset ( $this->data ['pid'] ) || isset ( $this->data ['ppass'] ) || isset ( $this->data ['action'] ) ) {
        foreach ( $partner_array as $pa ) {
          if ($pa ['id'] == $this->data ['pid'] && $pa ['secretkey'] == $this->data ['ppass']) {   
            return true;
          }
        }
      }
    }
    // default false
    return false;
  }

  function processRest() {

    /* LOG DATA REQUEST */
    // $writedata = date ( 'd/m/Y H:i:s', time () ) . ': ' . json_encode ( $this->request ['params'] ) . "\n";
    // file_put_contents ( dirname ( __FILE__ ) . '/../../client/test.request.txt', $writedata, FILE_APPEND | LOCK_EX );

    try {

      global $db; 

      //check action
      $action = strtolower ( $this->data ['action'] );

      switch ($action) {

        case 'getaccesstoken' :   

          if(!user::createAccessToken( $this->data['pid'], $this->data['ppass'])){
            throw new DSException ( - 9991, 'SYSTEM', 'Access Token can\'t create' );  
          }
          $this->response = DSException::setStringResult( $_SESSION['user']['token'] );

          break;

        case 'getcharactor' :   

          if(!user::createAccessToken( $this->data['pid'], $this->data['ppass'])){
            throw new DSException ( - 9991, 'SYSTEM', 'Access Token can\'t create' );  
          } 
          
          $this->response = DSException::setStringResult( $_SESSION['user']['token'], user::getCharinfo() );

          break;

        default :
          throw new DSException ( - 998, 'ERROR_REQUEST', 'Invalid Action', $_SESSION['user']['token'] );
          break;
      }
    } catch ( DSException $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->__toArray ();
    }
  }
}
